package com.dding.cms.base.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.javalite.activejdbc.Base;
import org.javalite.activejdbc.ColumnMetadata;
import org.javalite.instrumentation.Main;

import com.dding.cms.base.entity.MarkConfig;

public class ConstructBO {

	private static Map<String, String> mapping = new HashMap<String, String>();

	static {
		mapping.put("VARCHAR", "String");
		mapping.put("INT", "Integer");
		mapping.put("BIT", "Boolean");
		mapping.put("DATETIME", "java.util.Date");
		mapping.put("DATE", "java.util.Date");
	}

	public void generatorByDb(String clazz, String simpleName) throws Exception {
		Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dding",
				"root", "1234");
		StringBuilder builder = new StringBuilder();
		beforeBo(builder, simpleName);

		Class<?> c = Class.forName(clazz);
		Method m = c.getMethod("getMetaModel", new Class[] {});
		Object object = m.invoke(c, new Object[] {});

		Method second = object.getClass().getMethod("getColumnMetadata",
				new Class[] {});
		@SuppressWarnings("unchecked")
		Map<String, ColumnMetadata> map = (Map<String, ColumnMetadata>) second
				.invoke(object, new Object[] {});

		for (Entry<String, ColumnMetadata> entry : map.entrySet()) {
			processBo(builder, entry.getValue().getTypeName(), entry.getKey());

		}
		outBo(builder, simpleName);
	}

	public void generatorByProperties(String simpleName, String fileName)
			throws Exception {
		Properties properties = new Properties();
		InputStream stream = ConstructBO.class.getResourceAsStream(fileName);
		properties.load(stream);
		StringBuilder builder = new StringBuilder();
		beforeBo(builder, simpleName);

		for (Object key : properties.keySet()) {
			processBo(builder, (String) properties.getProperty((String) key),
					(String) key);
		}
		outBo(builder, simpleName);
		Base.close();
	}

	private void beforeBo(StringBuilder builder, String simpleName) {
		builder.append("package com.dding.cms.base.web.bo;");
		builder.append("public class " + simpleName + "BO" + "{");
	}

	private void processBo(StringBuilder builder, String type, String filed) {
		String value = mapping.get(type);
		if (value == null)
			value = "String";
		builder.append("private " + value + " " + filed + ";");
		builder.append("\n");
		String first = filed.substring(0, 1);
		builder.append("public " + value + " get" + first.toUpperCase()
				+ filed.substring(1) + "(){");
		builder.append("return " + filed + ";}");

		builder.append("public void set" + first.toUpperCase()
				+ filed.substring(1) + "(" + value + " " + filed + "){");
		builder.append("this." + filed + "=" + filed + ";}");
	}

	private void outBo(StringBuilder builder, String simpleName)
			throws Exception {
		builder.append("}");
		String name = "com.dding.cms.base.web.bo";
		URL url = ConstructBO.class.getResource("/");
		String path = new File(new File(url.getPath()).getParent()).getParent();
		String fileName = path + File.separator + "src" + File.separator
				+ toPath(name) + File.separator + simpleName + "BO.java";
		FileOutputStream out = new FileOutputStream(new File(fileName));
		out.write(builder.toString().getBytes());
		out.close();
		System.exit(0);
	}

	private static String toPath(String path) {
		return path.replace(".", "\\");
	}

	public static void main(String[] args) throws Exception {
		System.setProperty("outputDirectory",
				"D:/moduleeclipse/tip/assist/module/cmsBase/build/classes");
		Main.main(new String[] {});
		ConstructBO bo = new ConstructBO();
		 bo.generatorByDb(MarkConfig.class.getName(), "MarkConfig");
//		bo.generatorByProperties("ContentList", "data.properties");
	}
}
